<template>
  <el-dialog
    :title="toggleTitle"
    :close-on-click-modal="false"
    :visible.sync="visible"
    width="420px">
    <el-form
      ref="dataForm"
      :model="dataForm"
      :rules="rules"
      label-width="110px">
      <el-form-item label="码头作业类型" prop="actType">
        <el-input placeholder="码头作业类型" clearable v-model="dataForm.actType"></el-input>
      </el-form-item>
      <el-form-item label="费用名称" prop="name">
        <el-input placeholder="费用名称" clearable v-model="dataForm.name"></el-input>
      </el-form-item>
      <el-form-item label="20尺费用" prop="twentyCost">
        <el-input placeholder="20尺费用" clearable v-model.number="dataForm.twentyCost"></el-input>
      </el-form-item>
      <el-form-item label="40尺费用" prop="fortyCost">
        <el-input placeholder="40尺费用" clearable v-model.number="dataForm.fortyCost"></el-input>
      </el-form-item>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button @click="close">取消</el-button>
      <el-button type="primary" @click="submitForm">确定</el-button>
    </div>
  </el-dialog>
</template>

<script>
  // api
  import { add, detail, update } from '@/api/costSubsidies'
  // vuex
  import { mapGetters } from 'vuex'

  export default {
    name: 'add-or-update',
    data() {
      return {
        visible: false,
        dataForm: {
          id: '',
          actType: '',
          name: '',
          twentyCost: '',
          fortyCost: ''
        },
        rules: {
          actType: [
            { required: true, message: '必填项', trigger: 'blur' }
          ],
          name: [
            { required: true, message: '必填项', trigger: 'blur' }
          ],
          twentyCost: [
            { required: true, message: '必填项' },
            { type: 'number', message: '请输入数字' }
          ],
          fortyCost: [
            { required: true, message: '必填项' },
            { type: 'number', message: '请输入数字' }
          ],
        }
      }
    },
    computed: {
      toggleTitle() {
        return !this.dataForm.id ? '新增' : '修改'
      },
      ...mapGetters([
        'currentId'
      ])
    },
    methods: {
      init() {
        this.visible = true
        if (this.currentId && this.currentId.id) {
          detail({ id: this.currentId.id }).then((res) => {
            const { actType, name, twentyCost, fortyCost } = res.data.data
            this.dataForm = { actType, name, twentyCost, fortyCost }
          })
        } else {
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
          })
        }
      },
      submitForm() {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            const data = Object.assign({}, this.dataForm, this.currentId)
            if (!this.currentId.id) {
              // add...
              add(data).then((res) => {
                this._popMessage()
              })
            } else {
              // update...
              update(data).then((res) => {
                this._popMessage()
              })
            }
          } else {
            return false
          }
        })
      },
      close() {
        this.visible = false
        this.$refs['dataForm'].resetFields()
      },
      _popMessage() {
        this.$message({
          message: '操作成功',
          type: 'success'
        })
        this.visible = false
        this.$emit('refresh')
      }
    }
  }
</script>


<!-- Add "scoped" attribute to limit CSS to this component only -->
<style lang="scss" rel="stylesheet/scss" type="text/scss">

</style>
